package com.oig.common.util;

import cn.hutool.core.util.StrUtil;
import com.oig.common.constants.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class RequestHeaderUtil {

    private RequestHeaderUtil(){}

    public static String getToken(){
        HttpServletRequest request = SpringContextUtil.getRequest();
        if (request==null){
            log.debug("request is null");
            return null;
        }
        return request.getHeader(HttpHeaders.AUTHORIZATION) ;
    }

    public static Integer getSystemId(){
        String xData = getExpandData();
        if (StrUtil.isBlank(xData)){
            return 0;
        }
        String[] param = xData.split(CommonConstants.SPLIT_STR) ;
        if (param.length>0){
            return Integer.parseInt(param[0].trim()) ;
        }
        return 0;
    }

    public static Integer getResourceId(){
        String xData = getExpandData();
        if (StrUtil.isBlank(xData)){
            return null;
        }
        String[] param = xData.split(CommonConstants.SPLIT_STR) ;
        if (param.length>1){
            return Integer.parseInt(param[1].trim()) ;
        }
        return null;
    }


    public static String getExpandData(){
        HttpServletRequest request = SpringContextUtil.getRequest();
        if (request==null){
            log.debug("request is null");
            return null;
        }
        return request.getHeader(CommonConstants.EXPAND_DATA) ;
    }

}
